package god530.com.simpleapp_slide.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2016/12/19.
 */

public class DBHelper extends SQLiteOpenHelper {

    private static DBHelper helper;

    public static final SQLiteDatabase getSqliteDatabase(Context ctx) {
        if (helper == null) {
            helper = new DBHelper(ctx, DBContants.DBName, null, DBContants.DBVersion);
        }
        return helper.getWritableDatabase();

    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, null, version);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DBContants.SUBS.CREATE_TABLE);
        db.execSQL(DBContants.TYPES.CREATE_TABLE);
        db.execSQL(DBContants.DETAILS.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            db.execSQL(DBContants.SUBS.DROP_TABLE);
            db.execSQL(DBContants.TYPES.DROP_TABLE);
            db.execSQL(DBContants.DETAILS.DROP_TABLE);

            db.execSQL(DBContants.SUBS.CREATE_TABLE);
            db.execSQL(DBContants.TYPES.CREATE_TABLE);
            db.execSQL(DBContants.DETAILS.CREATE_TABLE);
        }
    }
}
